package com.surprise.db.dao.join;

import org.springframework.stereotype.Repository;

import com.surprise.db.dao.SimpleDAO;
import com.surprise.db.entity.join.SelectPMThingsMst002Entity;

/**
 * SelectPMThingsMst002.java
 * @author xuling
 * @since 2017年2月19日
 */
@Repository
public class SelectPMThingsMst002 extends SimpleDAO<SelectPMThingsMst002Entity> {
	
	/**
	 * 获取指定的那些事
	 * @param userid 用户ID
	 * @param id 那些事ID
	 * @return 指定商场的那些事
	 */
	public SelectPMThingsMst002Entity select(Long userid, Long id) {
		String sql = "select pm_things_mst.id, pm_things_mst.userid, user.username as name, " +
					 " user.portrait as portrait, pm_things_mst.content, pm_things_mst.titleimg, " +
					 " pm_things_mst.detailimg, pm_things_mst.addtime, " +
					 " pm_things_mst.address, mall.name as mallName, " +
					 " COUNT(DISTINCT pm_things_like_mst.id) as likeCount, " +
					 " COUNT(DISTINCT pm_things_comment_mst.id) as commentCount, " +
					 " COUNT(DISTINCT pm_things_share_mst.id) as shareCount, " +
					 " COUNT(DISTINCT likeB.id) as userLikeCount " +
					 " from pm_things_mst " +
					 " LEFT JOIN user on pm_things_mst.userid = user.id " +
					 " LEFT JOIN pm_things_like_mst on pm_things_mst.id = pm_things_like_mst.thingsid " + 
					 " and pm_things_like_mst.deleteflg = 0" +
					 " LEFT JOIN pm_things_comment_mst on pm_things_mst.id = pm_things_comment_mst.thingsid " +
					 " LEFT JOIN pm_things_share_mst on pm_things_mst.id = pm_things_share_mst.thingsid " + 
					 " and pm_things_share_mst.deleteflg = 0 " + 
					 " LEFT JOIN pm_things_like_mst likeB on pm_things_mst.id = likeB.thingsid " + 
					 " and likeB.userid = ? and likeB.deleteflg = 0 " + 
					 " LEFT JOIN mall on pm_things_mst.mallid = mall.id" +
					 " WHERE pm_things_mst.id = ? ";
		return super.findOneBySql(sql, userid, id);
	}
}
